#coding=utf-8
"""
Author:林怡
Time:2025/5/7 09:57
"""

'''
此模块是用来封装所有的用例的基类
定位方法  输入  点击
'''
import sys
sys.path.append('..')
import unittest
from time import *

# 调试代码
# from selenium import webdriver
# driver = webdriver.Chrome()


class BasePage(unittest.TestCase):

    @classmethod
    def set_driver(cls,driver):
        cls.driver = driver

    @classmethod
    def get_driver(cls):
        return cls.driver  # 单例设计模式

    # baidu_input=('id','kw')
    @classmethod
    def find_element(cls,element):
        type = element[0]  #id
        value = element[1] #kw
        if type =="id":
            elem = cls.driver.find_element_by_id(value)
        elif type == "xpath":
            elem = cls.driver.find_element_by_xpath(value)
        elif type == "name":
            elem = cls.driver.find_element_by_name(value)
        elif type == 'class':
            elem = cls.driver.find_element_by_class_name(value)
        elif type == "css":
            elem = cls.driver.find_element_by_css_selector(value)
        elif type =="link_text":
            elem = cls.driver.find_element_by_link_text(value)
        elif type=="partial":
            elem = cls.driver.find_element_by_partial_link_text(value)
        else: #如果不是上面的定位方式则手动触发异常
            raise  ValueError("定位方式有误摸清输入正确的参数")
        return elem

    @classmethod  #封装输入函数
    def sendKeys(cls,elem,text):
        return elem.send_keys(text)

    @classmethod #封装的点击操作
    def click(cls,elem):
        return elem.click()

    @classmethod  #封装隐式等待
    def wait(cls,sec):
        return cls.driver.implicitly_wait(sec)

    @classmethod #封装切换iframe框
    def frame(cls,elem):
        return cls.driver.switch_to.frame(elem)

    @classmethod  #退出iframe框
    def outframe(cls):
        return cls.driver.switch_to.default_content()

    @classmethod
    def sleep(cls,sec):
        return sleep(sec)

    @classmethod #封装的删除操作
    def clear(cls,elem):
        return elem.clear()


    @classmethod
    def get_text(cls,elem):
        '''封装根据网页元素获取文本信息'''
        value = BasePage.find_element(elem).text
        return value
if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.maximize_window()
    baidu_input =('id','kw')
    BasePage.sleep(3)
    # elem = BasePage.find_element(baidu_input).send_keys("123")
    elem = BasePage.find_element(baidu_input)
    BasePage.sendKeys(elem,'dcs')


























